package com.example.tool.activtiy;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.mvp.CommonPresent;

import butterknife.BindView;

/**
 * Created Date 2019/8/30
 *
 * @author liangkun
 * description：帧动画
 * 帧动画比较容易引起OOM，所以在使用帧动画时
 */
public class FramAnimActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.card_home_item)
    protected CardView cardOne;
    @BindView(R.id.card_home_two)
    protected CardView cardTwo;

    @BindView(R.id.iv_anim)
    protected ImageView ivAnim;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    private AnimationDrawable animationDrawable;

    @Override
    protected int getLayoutId() {
        return R.layout.fram_anim;
    }

    @Override
    protected void initView() {
        tvTitle.setText("帧动画");
        // 通过逐帧动画的资源文件获得AnimationDrawable示例
        animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.fram_anim);
        // 把AnimationDrawable设置为ImageView的背景
        ivAnim.setBackground(animationDrawable);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
        cardOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                start();
            }
        });
        cardTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop();
            }
        });

    }


    protected void start() {
        if (animationDrawable != null && !animationDrawable.isRunning()) {//判断
            animationDrawable.start();
        }
    }

    protected void stop() {
        if (animationDrawable != null && animationDrawable.isRunning()) {
            animationDrawable.stop();
        }
    }


    @Override
    protected CommonPresent createPresenter() {
        return null;
    }

}
